package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.manor.FishPondData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/10/14 10:47
 */
public class UserFishPondDataDaoImpl extends TempDataObjectDao<FishPondData> {
    public static String insertSql = "insert into t_u_fishpond_data(userId,recoveryTime,fishPondScore," +
            "fishPondLv,fishingState,fishingJudgeTime,judgeFishId,judgeFishWeight,exp,lastRecycleTime,autoFinishTask) values(?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(FishPondData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRecoveryTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFishPondScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFishPondLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFishingState()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFishingJudgeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getJudgeFishId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getJudgeFishWeight()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getExp()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecycleTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isAutoFinishTask()));

        return getDb().executeNoneQuery(insertSql, para) > 0;
    }
    public static String updateSql = "update t_u_fishpond_data set recoveryTime=?,fishPondScore=?," +
            "fishPondLv=?,fishingState=?,fishingJudgeTime=?,judgeFishId=?,judgeFishWeight=?,exp=?,lastRecycleTime=?,autoFinishTask=? where userId=?";
    @Override
    protected boolean updateData(FishPondData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getRecoveryTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFishPondScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFishPondLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFishingState()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFishingJudgeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getJudgeFishId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getJudgeFishWeight()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getExp()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecycleTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isAutoFinishTask()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        return getDb().executeNoneQuery(updateSql, para) > 0;
    }

    @Override
    protected FishPondData parseData(ResultSet rs) throws SQLException {
        FishPondData data = new FishPondData();
        data.setUserId(rs.getLong("userId"));
        data.setRecoveryTime(rs.getLong("recoveryTime"));
        data.setFishPondScore(rs.getLong("fishPondScore"));
        data.setFishPondLv(rs.getInt("fishPondLv"));
        data.setFishingState(rs.getInt("fishingState"));
        data.setFishingJudgeTime(rs.getLong("fishingJudgeTime"));
        data.setJudgeFishId(rs.getInt("judgeFishId"));
        data.setJudgeFishWeight(rs.getLong("judgeFishWeight"));
        data.setExp(rs.getLong("exp"));
        data.setLastRecycleTime(rs.getLong("lastRecycleTime"));
        data.setAutoFinishTask(rs.getBoolean("autoFinishTask"));
        return data;
    }

    public static String getSql = "select * from t_u_fishpond_data where userId = ?";

    public FishPondData getUserFishPondData(long playerId) {
        ResultSet rs = null;
        CallableStatement call = null;
        FishPondData data = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            call.setLong(1, playerId);
            rs = call.executeQuery();
            while (rs.next()) {
                data = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return data;
    }
}
